<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: boxes.php 502 2014-01-31 15:50:06Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 502 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-31 15:50:06 +0000 (Fri, 31 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require(DIR_FS_INC.'inc.category_tree.php');

$smarty = new Smarty();

define('DIR_WS_BOXES', DIR_FS_CATALOG .'templates/'.CURRENT_TEMPLATE. '/source/boxes/');
define('TEMPLATE_SNIPPETS', DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE. '/html/');
define('FORCE_CACHE',true);
$smarty->assign('tpl_path','templates/'.CURRENT_TEMPLATE.'/');
$smarty->assign('html_tpl_path', CURRENT_TEMPLATE.'/html/');

define('PRODUCT_ID', $_GET['products_id']);
define('CAT_ID', $_GET['cPath']);
define('CONTENT_ID', $_GET['coID']);
define('BLOG_CAT', $_GET['blog_cat']);
define('BLOG_ITEM', $_GET['blog_item']);
if($PHP_SELF == 'blog.php')
	define('IS_BLOG', 'true');

function getValue($Input) {
	$Value = preg_replace("/(&[^;]*;)/", '', $Input);
	$Value = preg_replace("/[^0-9]/", '', $Value);
	return ($Value/100);
}

function getBoxesPerPosition($pos, $is_blog = false) {
	global $db;
	
	if(GROUP_CHECK == 'true')
		$gc = "AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_g%' ";
	
	if(!$is_blog)
		$count = $db->db_query("SELECT id FROM ".TABLE_BOXES." WHERE position = '".$pos."' AND status = '1' ".$gc, true);
	else
		$count = $db->db_query("SELECT id FROM ".TABLE_BOXES." WHERE position = '".$pos."' AND status = '1' AND in_blog = '1' ".$gc, true);
	return $count->_numOfRows;
}

function countBoxesPostion() {
	global $db;
	$pos = $db->db_query("SELECT id, position_name FROM ".TABLE_BOXES_POSITION." ORDER BY id", true);
	$pos_array = array();
	while(!$pos->EOF){
		$pos_array[] = $pos->fields['position_name'];
		$pos->MoveNext();
	}

	foreach($pos_array as $pos) {
		if (defined('GLOBAL_COLUMN_'.strToUpper($pos)) 
				&& constant('GLOBAL_COLUMN_'.strToUpper($pos)) == 0)
			unset($pos_array[array_search($pos, $pos_array)]);
	}			
			
//	if(GLOBAL_COLUMN_LEFT==0) unset($pos_array[array_search('left', $pos_array)]);
//	if(GLOBAL_COLUMN_RIGHT==0) unset($pos_array[array_search('right', $pos_array)]);
//	if(GLOBAL_COLUMN_TOP==0) unset($pos_array[array_search('top', $pos_array)]);
//	if(GLOBAL_COLUMN_BOTTOM==0) unset($pos_array[array_search('bottom', $pos_array)]);
	return $pos_array;
}

function getBoxTitle($box_name) {
	global $db;
	$title = '';
	
	$name = $db->db_query("SELECT box_title FROM ".TABLE_BOXES_NAMES." WHERE box_name = '".$box_name."' AND language_id = '".$_SESSION['languages_id']."' AND status = '1'", true);

	if($name->_numOfRows) {
		$bs = $db->db_query("SELECT border_color,background_content,color_content,background_head,background_head,color_head FROM ".TABLE_BOXES_STYLES." WHERE box_name = '".$box_name."'", true);

		$title = '<div class="box" style="'.(($bs->fields['border_color']!='')?'border-color:#'.$bs->fields['border_color'].';':'').(($bs->fields['background_content']!='')?'background:#'.$bs->fields['background_content'].';':'').(($bs->fields['color_content']!='')?'color:#'.$bs->fields['color_content'].';':'').'">'."\n";
			
		$title .= '<div class="infoBoxHeading" style="'.(($bs->fields['background_head']!='')?'background:#'.$bs->fields['background_head'].';':'').(($bs->fields['color_head']!='')?'color:#'.$bs->fields['color_head'].';':'').'">'."\n";
		$title .= $name->fields['box_title'];
		$title .= '</div>';
		return $title;
	} else
		return $title;
}

function getBoxContent($box_name) {
	global $db;
	if($data = $db->db_query("SELECT box_desc FROM ".TABLE_BOXES_NAMES." WHERE box_name = '".$box_name."' AND language_id = '".$_SESSION['languages_id']."'", true))
		return $data->fields['box_desc'];
	else
		return;
}

function getBoxFlag($box_name) {
	global $db;
	if($data = $db->db_query("SELECT file_flag FROM ".TABLE_BOXES." WHERE box_name = '".$box_name."'", true)) {
		$check = $db->db_query("SELECT content_id FROM ".TABLE_CONTENT_MANAGER." WHERE file_flag = '".$data->fields['file_flag']."'", true);
		if($check->_numOfRows)
			return $data->fields['file_flag'];
	}
	else
		return false;
}

$boxes = countBoxesPostion();

if(!(
	(
		strstr($_SERVER['PHP_SELF'], FILENAME_SHOPPING_CART) 
		|| strstr($_SERVER['PHP_SELF'], FILENAME_CHECKOUT_SHIPPING) 
		|| strstr($_SERVER['PHP_SELF'], FILENAME_CHECKOUT_CONFIRMATION) 
		|| strstr($_SERVER['PHP_SELF'], FILENAME_CHECKOUT_PAYMENT) 
		|| strstr($_SERVER['PHP_SELF'], FILENAME_CHECKOUT_PAYMENT_ADDRESS) 
		|| strstr($_SERVER['PHP_SELF'], FILENAME_CHECKOUT_SHIPPING_ADDRESS) 
		|| strstr($_SERVER['PHP_SELF'], FILENAME_CHECKOUT_PAYMENT_ADDRESS) 
		|| strstr($_SERVER['PHP_SELF'], FILENAME_CHECKOUT_SUCCESS)
	) 
	&& isset($_SESSION['customer_id']) 
	&& BOXLESS_CHECKOUT == 'true') 
	&& (BLOG_STAND_ALONE != 'true' || (!isset($_GET['blog_cat']) && !isset($_GET['blog_item']) && !isset($_GET['m']) && !isset($_GET['y'])))) {

		if(GROUP_CHECK == 'true')
			$gc = " AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_g%' ";
	
	foreach($boxes AS $name) {
		$get_box = $db->db_query("SELECT box_name, position, sort_id, box_type, status, num FROM ".TABLE_BOXES." WHERE position = '".$name."' AND status = '1'".$gc." ORDER BY sort_id ASC", true);
		if($get_box->_numOfRows){
			${$name} = '';
			while(!$get_box->EOF){
				$box_content = '';
				if($get_box->fields['box_type'] == 'file') {
					if(strstr($get_box->fields['box_name'], 'categories_') && is_numeric(substr($get_box->fields['box_name'], 11)))
						include(DIR_WS_BOXES.'categories.php');
					else
						include(DIR_WS_BOXES.strtolower($get_box->fields['box_name']).'.php');

				} elseif($get_box->fields['box_type'] == 'database') {
					$box_name = '';
					$box_name = $get_box->fields['box_name'];
					include(DIR_WS_BOXES.'box.php');

				} else
					echo 'Beim einbinden der Boxen ist ein Fehler aufgetreten!';

				${$name} .= $box_content;
				$get_box->MoveNext();
			}
			$smarty->assign('BOXES_'.$name, ${$name});
			$smarty->assign('BOXES_'.$name.'_count', getBoxesPerPosition($name));
		}
	}
	
} elseif(isset($_GET['blog_cat']) || isset($_GET['blog_item']) || isset($_GET['m']) || isset($_GET['y'])) {
	foreach($boxes AS $name) {
		$get_box = $db->db_query("SELECT box_name, position, sort_id, box_type, status, num FROM ".TABLE_BOXES." WHERE position = '".$name."' AND status = '1' AND in_blog = '1'".$gc." ORDER BY sort_id ASC", true);

		if($get_box->_numOfRows){
			${$name} = '';

			while(!$get_box->EOF){
				$box_content = '';
				if($get_box->fields['box_type'] == 'file') {
					if(strstr($get_box->fields['box_name'], 'categories_') && is_numeric(substr($get_box->fields['box_name'], 11)))
						include(DIR_WS_BOXES.'categories.php');
					else
						include(DIR_WS_BOXES.strtolower($get_box->fields['box_name']).'.php');

				} elseif($get_box->fields['box_type'] == 'database') {
					$box_name = '';
					$box_name = $get_box->fields['box_name'];
					include(DIR_WS_BOXES.'box.php');

				} else
					echo 'Beim einbinden der Boxen ist ein Fehler aufgetreten!';

				${$name} .= $box_content;
				$get_box->MoveNext();
			}
			$smarty->assign('BOXES_'.$name,${$name});
			$smarty->assign('BOXES_'.$name.'_count', getBoxesPerPosition($name, true));
		}
	}
}
//echo($browser->getVersion() . ' .. ' . $browser->getBrowser());
if(($browser->getVersion() < 9) && ($browser->getBrowser() == 'Internet Explorer'))
	$smarty->assign('browsertest', BROWSER_TEST);

$smarty->assign('copyright','&copy; '.date('Y').' - <a title="'.STORE_NAME.'" href="'.DIR_WS_CATALOG.'">'.STORE_NAME.'</a>');

if (GROUP_CHECK == 'true')
    $group_check = " AND c.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 ";